package 作业一;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.LinkedList;
//import java.util.List;

import java.util.List;
import java.util.Stack;

import org.junit.Test;

import java.util.LinkedList;
//import java.util.List;


import java.util.List;
import java.util.Stack;

import org.junit.Test;
//

public class Minmese {
	//n个部件,m个工厂
	static  int WEIGHT=10000;
	static Thing[] LIST;//最优解

//	static 	Stack<LinkedList<Thing>> stack=new Stack<LinkedList<Thing>>();
	private static  ArrayList[] init(int n,int m){
		ArrayList[] list=new ArrayList[n];//每个list数组存放所有工厂的第n个部件信息

		for(int j=0;j<n;j++){
			ArrayList<Thing> l=new ArrayList<Thing>();
			for(int i=0;i<m;i++){
				Thing thing=new Thing();
				//产生0-10的单精度

				thing.setValue((int)(Math.random()*140));
				//产生0-20的单精度

				thing.setWeight((int)(Math.random()*80));
				l.add(thing);
			}
			list[j]=l;
		}
		return list;
	}
	//

	//找出最优值

	public static void made( ArrayList[] l){
//		Stack<ArrayList<Thing>> stack=new Stack<ArrayList<Thing>>();
		int n=0;
		ArrayList<Thing> list=new ArrayList<Thing>(); 
		for(int m=0;m<l[n].size();m++)
		{
			
			list.add((Thing) l[n].get(m));
			 n++;	 
			 seach( l, n, list);
//			 System.out.println(stack.get(0).get(0).getValue());
//			 System.out.println(list.size()+"L"+LIST.length);
			 list.remove(list.size()-1);
//			 System.out.println(list.size()+"L"+LIST.length);
//			 System.out.println(stack.get(0).get(0).getValue());
			 n--;
			
		}
		
		
	}
	//递归

	public static void seach(ArrayList[] l,int n,ArrayList<Thing> list)
	{
		if(n==l.length-1){
			for(int m=0;m<l[n].size();m++)
			{
				list.add((Thing) l[n].get(m));
				getL(list);
				list.remove(list.size()-1);
			}
	
		}
		else
		for(int m=0;m<l[n].size();m++)
		{
//			if(m!=0){

//				list.removeLast();

//			}	

			list.add((Thing) l[n].get(m));
//			System.out.println(list.getLast().getClass());
			 n++;
			seach( l, n, list);
			
			list.remove(list.size()-1);
			n--;
		}
	
//		System.out.println(stack.get(0).get(0).getValue());
	}
	public static void getL(ArrayList<Thing>  list){
		int value=0;
		int weight=0;
		for(Thing thing:list){
			value+=thing.getValue();
			weight+=thing.getWeight();
		}
		if(value<=200 && weight<WEIGHT){
//			ArrayList<Thing>  list1=new ArrayList<Thing>();
//			list1=list;
			LIST=new Thing[list.size()];
			for(int i=0;i<list.size();i++)
				LIST[i]=list.get(i);
			//LIST=list;
//			System.out.println(LIST+"  "+list);
			WEIGHT=weight;
		}
	}
	public static void main(String[] args) {
		ArrayList[] l=init(2, 2);
		int i=1;
	for( ArrayList<Thing> L:l)
	{	System.out.println();
		System.out.println("第"+i+"个产品");
		for(Thing t:L)
		System.out.print("价值"+t.getValue()+"重量"+t.getWeight()+"\t");
		i++;
	}
		made(l);
		System.out.println("\n"+WEIGHT);
		
		for(Thing t:LIST){
			System.out.println(t.getValue() +"+++"+t.getWeight());
		}
//		System.out.println(WEIGHT);
		
//		System.out.println(stack.get(0).get(1).getValue());
	}
//	@Test
//	public void test()
//	{
//		 LinkedList[] l=init(2, 2);
////		 int i=1;
////		System.out.println(l.length);
//
////		for( LinkedList<Thing> L:l)
////		{	System.out.println();
////			System.out.println("第"+i+"个产品");
////			for(Thing t:L)
////			System.out.print("价值"+t.getValue()+"重量"+t.getWeight()+"\t");
////			i++;
////		}
//		Stack<LinkedList<Thing>> stack=made(l);
////		System.out.println(stack.get(0).get(0).getValue());
////		for(int i=0;i<stack.size();i++){
////			System.out.println(i);
////			for(int j=0;j<stack.get(i).size();j++){
////				System.out.println(j);
////				//System.out.println(stack.get(i).get(j).getWeight()+"zhong");
////			}
////		}
//		System.out.println(stack.size());
//	}
}
